home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SRC / WGIF.C next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  14.4 KB  |  611 lines

  1. /*                       GIF Load/Save Source for WGT
  2.                                   Free code
  3.  
  4. Notes:  There are now restrictions on the use of the GIF file format.
  5.         Royalties must be paid to CompuServe if the program you have created
  6.         is sold for profit. Freeware programs may use this code without
  7.         any payments.
  8.  
  9.         Exact details can be obtained from CompuServe.
  10. */
  11.  
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <mem.h>
  16. #include <dos.h>
  17. #include <malloc.h>
  18. #include <wgt5.h>
  19.  
  20.  
  21. #define GOOD_READ       0
  22. #define BAD_FILE        1
  23. #define BAD_READ        2
  24. #define UNEXPECTED_EOF  3
  25. #define BAD_CODE        4
  26. #define BAD_FIRSTCODE   5
  27. #define BAD_ALLOC       6
  28. #define BAD_SYMBOLSIZE  7
  29.  
  30. #define NO_CODE         -1
  31.  
  32. #define largest_code    4095
  33. #define table_size      5003
  34.  
  35.  
  36. typedef struct {
  37.         signed char sig[6];
  38.         unsigned short screenwidth, screendepth;
  39.         unsigned char flags, background, aspect;
  40.         } GIFHEADER;
  41.  
  42. typedef struct {
  43.         unsigned short left, top, width, depth;
  44.         unsigned char flags;
  45.         } IMAGEBLOCK;
  46.  
  47. typedef struct {
  48.         short width, depth, bits;
  49.         short flags;
  50.         short background;
  51.         signed char palette[768];
  52.         short (*setup)();
  53.         short (*closedown)();
  54.         short (*saveline)();
  55.         short (*saveext)();
  56.         } FILEINFO;
  57.  
  58. typedef struct {
  59.         signed char blocksize;
  60.         signed char flags;
  61.         unsigned short delay;
  62.         signed char transparent_colour;
  63.         signed char terminator;
  64.         } CONTROLBLOCK;
  65.  
  66. typedef struct {
  67.         signed char blocksize;
  68.         unsigned short left, top;
  69.         unsigned short gridwidth, gridheight;
  70.         signed char cellwidth, cellheight;
  71.         signed char forecolour, backcolour;
  72.         } PLAINTEXT;
  73.  
  74. typedef struct {
  75.         signed char blocksize;
  76.         signed char applstring[8];
  77.         signed char authentication[3];
  78.         } APPLICATION;
  79.  
  80. static char code_buffer[259];
  81. static FILEINFO fi;
  82. static block ptr;
  83. static int pixctr;
  84. static int maxctr;
  85. static short oldcode[table_size];
  86. static short currentcode[table_size];
  87. static char newcode[table_size];
  88. static short code_size;
  89. static short clear_code;
  90. static short eof_code;
  91. static short bit_offset;
  92. static short byte_offset;
  93. static short bits_left;
  94. static short max_code;
  95. static short free_code;
  96.  
  97. void putline (block p, int n)
  98. {
  99.   if (n >= 0 && n < fi.depth)
  100.     memcpy (ptr + ((int)n * (int)fi.width) + 4, p, fi.width);
  101. }
  102.  
  103.  
  104. void putextension (FILE *fp)
  105. {
  106.   PLAINTEXT pt;
  107.   CONTROLBLOCK cb;
  108.   APPLICATION ap;
  109.   short c, n, i;
  110.  
  111.   fread (&c, 1, 1, fp);
  112.   switch (c) {
  113.     case 0x0001 : fread ((char *)&pt, 1, sizeof(PLAINTEXT), fp);
  114.                   do {
  115.                     if ((n = fgetc(fp)) != EOF)
  116.                     {
  117.                       for (i = 0; i < n; i++)
  118.                         fgetc (fp);
  119.                     }
  120.                   } while (n > 0 && n != EOF);
  121.                   break;
  122.     case 0x0009 : fread ((char *)&cb, 1, sizeof(CONTROLBLOCK), fp);
  123.                   break;
  124.     case 0x00fe : do {
  125.                     if ((n = fgetc (fp)) != EOF)
  126.                     {
  127.                       for (i = 0; i < n; i++)
  128.                         fgetc (fp);
  129.                     }
  130.                   } while (n > 0 && n != EOF);
  131.                   break;
  132.     case 0x00ff : fread ((char *)&ap, 1, sizeof(APPLICATION), fp);
  133.                   do {
  134.                     if ((n = fgetc (fp)) != EOF)
  135.                     {
  136.                       for (i = 0; i < n; i++)
  137.                         fgetc (fp);
  138.                     }
  139.                   } while (n > 0 && n != EOF);
  140.                   break;
  141.     default :     if ((n = fgetc (fp)) != EOF)
  142.                   {
  143.                     for (i = 0; i < n; i++)
  144.                       fgetc (fp);
  145.                   }
  146.                   break;
  147.   }
  148. }
  149.  
  150.  
  151. short unpackimage (FILE *fp, short bits)
  152. {
  153.   short bits2;
  154.   short codesize;
  155.   short codesize2;
  156.   short nextcode;
  157.   short thiscode;
  158.   short oldtoken;
  159.   short currentcode;
  160.   short oldcode;
  161.   short bitsleft;
  162.   short blocksize;
  163.   short line = 0;
  164.   short byte = 0;
  165.   short pass = 0;
  166.  
  167.   char *p;
  168.   char *q;
  169.   char b[255];
  170.   char *u;
  171.   char *linebuffer;
  172.  
  173.   static char firstcodestack[4096];
  174.   static char lastcodestack[4096];
  175.   static short codestack[4096];
  176.  
  177.   static short wordmasktable[] = { 0x0000, 0x0001, 0x0003, 0x0007,
  178.                                    0x000f, 0x001f, 0x003f, 0x007f,
  179.                                    0x00ff, 0x01ff, 0x03ff, 0x07ff,
  180.                                    0x0fff, 0x1fff, 0x3fff, 0x7fff };
  181.  
  182.   static short inctable[] = { 8, 8, 4, 2, 0 };
  183.   static short startable[] = { 0, 4, 2, 1, 0 };
  184.  
  185.   p = q = b;
  186.   bitsleft = 8;
  187.  
  188.   if (bits < 2 || bits > 8)
  189.     return (BAD_SYMBOLSIZE);
  190.   bits2 = 1 << bits;
  191.   nextcode = bits2 + 2;
  192.   codesize2 = 1 << (codesize = bits + 1);
  193.   oldcode = oldtoken = NO_CODE;
  194.  
  195.   if ((linebuffer = malloc (fi.width)) == NULL)
  196.     return (BAD_ALLOC);
  197.  
  198.   /* loop until something breaks */
  199.   for (;;) {
  200.     if (bitsleft == 8) {
  201.       if (++p >= q && (((blocksize = fgetc(fp)) < 1) ||
  202.          (q=(p=b)+fread (b, 1, blocksize, fp)) < (b+blocksize))) {
  203.         free (linebuffer);
  204.         return (UNEXPECTED_EOF);
  205.       }
  206.       bitsleft = 0;
  207.     }
  208.     thiscode = *p;
  209.     if ((currentcode = (codesize+bitsleft)) <= 8) {
  210.       *p >>= codesize;
  211.       bitsleft = currentcode;
  212.     }
  213.     else {
  214.       if (++p >= q && (((blocksize = fgetc(fp)) < 1) ||
  215.          (q=(p=b)+fread (b, 1, blocksize, fp)) < (b+blocksize))) {
  216.         free (linebuffer);
  217.         return (UNEXPECTED_EOF);
  218.       }
  219.       thiscode |= *p << (8 - bitsleft);
  220.       if (currentcode <= 16)
  221.         *p >>= (bitsleft = currentcode - 8);
  222.       else {
  223.         if (++p >= q && (((blocksize = fgetc(fp)) < 1) ||
  224.            (q=(p=b)+fread (b, 1, blocksize, fp)) < (b+blocksize))) {
  225.           free (linebuffer);
  226.           return (UNEXPECTED_EOF);
  227.         }
  228.         thiscode |= *p << (16 - bitsleft);
  229.         *p >>= (bitsleft = currentcode - 16);
  230.       }
  231.     }
  232.     thiscode &= wordmasktable[codesize];
  233.     currentcode = thiscode;
  234.  
  235.     if (thiscode == (bits2+1))
  236.       break;
  237.     if (thiscode > nextcode) {
  238.       free (linebuffer);
  239.       return (BAD_CODE);
  240.     }
  241.  
  242.     if (thiscode == bits2) {
  243.       nextcode = bits2 + 2;
  244.       codesize2 = 1 << (codesize = (bits + 1));
  245.       oldtoken = oldcode = NO_CODE;
  246.       continue;
  247.     }
  248.  
  249.     u = firstcodestack;
  250.  
  251.     if (thiscode == nextcode) {
  252.       if (oldcode == NO_CODE) {
  253.         free (linebuffer);
  254.         return (BAD_FIRSTCODE);
  255.       }
  256.       *u++ = oldtoken;
  257.       thiscode = oldcode;
  258.     }
  259.  
  260.     while (thiscode >= bits2) {
  261.       *u++ = lastcodestack [thiscode];
  262.       thiscode = codestack[thiscode];
  263.     }
  264.  
  265.     oldtoken = thiscode;
  266.     do {
  267.       linebuffer[byte++] = thiscode;
  268.       if (byte >= fi.width) {
  269.         putline (linebuffer, line);
  270.         byte = 0;
  271.         /* check for interlaced image */
  272.         if (fi.flags & 0x40) {
  273.           line += inctable[pass];
  274.           if (line >= fi.depth)
  275.             line = startable[++pass];
  276.         } else ++line;
  277.       }
  278.       if (u <= firstcodestack)
  279.         break;
  280.       thiscode = *--u;
  281.     } while (1);
  282.  
  283.     if (nextcode < 4096 && oldcode != NO_CODE) {
  284.       codestack[nextcode] = oldcode;
  285.       lastcodestack[nextcode] = oldtoken;
  286.       if (++nextcode >= codesize2 && codesize < 12)
  287.         codesize2 = 1 << ++codesize;
  288.     }
  289.     oldcode = currentcode;
  290.   }
  291.   free (linebuffer);
  292.   return (GOOD_READ);
  293. }
  294.  
  295.  
  296. block wloadgif (char *filename, color pal[256])
  297. {
  298.   GIFHEADER gh;
  299.   IMAGEBLOCK iblk;
  300.   int t;
  301.   short b,c;
  302.  
  303.   if  (wgtlibrary == NULL)
  304.   {
  305.     if  ((libf = fopen (filename, "rb")) == NULL)
  306.       return NULL;
  307.   }
  308.   else
  309.   {
  310.     if  ((libf = fopen (wgtlibrary, "rb")) == NULL)
  311.       return NULL;
  312.     readheader ();
  313.     findfile (filename);
  314.     if  (lresult == 1)
  315.       fseek (libf, lfpos, SEEK_SET);
  316.     if  (checkpassword (password) == 0)
  317.     {
  318.       wsetmode (3);
  319.       printf ("Incorrect password");
  320.       exit (1);
  321.     }
  322.   }
  323.   
  324.   if  ((wgtlibrary != NULL) & (lresult == 0)) goto lblkstop;
  325.  
  326.   if (fread((char *)&gh, 1, sizeof(GIFHEADER), libf) != sizeof(GIFHEADER) ||
  327.       memcmp(gh.sig, "GIF", 3)) 
  328.   {
  329.     fclose (libf);
  330.     return NULL;
  331.   }
  332.  
  333.   /* get screen dimensions */
  334.   fi.width = gh.screenwidth;
  335.   fi.depth = gh.screendepth;
  336.   fi.bits = (gh.flags & 0x0007) + 1;
  337.   /* get colour map if there is one */
  338.   if (gh.flags & 0x80) {
  339.     c = 3 * (1 << ((gh.flags & 7) + 1));
  340.     if (fread (pal, 1, c, libf) != c) 
  341.     {
  342.       fclose (libf);
  343.       return NULL;
  344.     }
  345.   }
  346.   /* step through the blocks */
  347.   while ((c=fgetc(libf))==',' || c=='!' || c==0) {
  348.     /* if it's an image block */
  349.     if (c == ',') {
  350.       /* get the start of the image block */
  351.       if (fread (&iblk, 1, sizeof(IMAGEBLOCK), libf) != sizeof(IMAGEBLOCK))
  352.       {
  353.         fclose (libf);
  354.         return NULL;
  355.       }
  356.  
  357.       /* get the image dimensions */
  358.       fi.width = iblk.width;
  359.       fi.depth = iblk.depth;
  360.  
  361.       /* get the local colour map if there is one */
  362.       if (iblk.flags & 0x80) {
  363.         b = 3 * (1 << ((iblk.flags & 0x0007) + 1));
  364.         if (fread (pal, 1, b, libf) != c)
  365.         {
  366.           fclose (libf);
  367.           return NULL;
  368.         }
  369.         fi.bits = (iblk.flags & 0x0007) + 1;
  370.       }
  371.  
  372.       /* get the initial code size */
  373.       if (fread (&c, 1, 1, libf) == 0)
  374.       {
  375.         fclose (libf);
  376.         return NULL;
  377.       }
  378.  
  379.       fi.flags = iblk.flags;
  380.  
  381.       if ( (ptr = malloc ( (int)fi.width * (int)fi.depth + 5) ) == NULL)
  382.       {
  383.         fclose (libf);
  384.         return NULL;
  385.       }
  386.  
  387.       for (b = 0; b < 256; b++)
  388.       {
  389.         pal[b].r >>= 2;
  390.         pal[b].g >>= 2;
  391.         pal[b].b >>= 2;
  392.       }
  393.       *(short *)ptr = fi.width;
  394.       ptr += 2;
  395.       *(short *)ptr = fi.depth;
  396.       ptr -= 2;
  397.       t = unpackimage (libf, c);
  398.       if (t != GOOD_READ)
  399.       {
  400.         free (ptr);
  401.         fclose (libf);
  402.         return NULL;
  403.       }
  404.       else {
  405.         fclose (libf);
  406.         return ptr;
  407.       }
  408.     }
  409.     else if (c == '!')
  410.       putextension (libf);
  411.   };
  412.   lblkstop:
  413.   ;
  414.   fclose (libf);
  415.   return ptr;
  416. }
  417.  
  418.  
  419. void init_table (short min_code_size)
  420. {
  421.   short i;
  422.  
  423.   code_size = min_code_size + 1;
  424.   clear_code = (1<<min_code_size);
  425.   eof_code = clear_code + 1;
  426.   free_code = clear_code + 2;
  427.   max_code = (1 << code_size);
  428.  
  429.   for (i=0; i<table_size; i++)
  430.     currentcode[i] = 0;
  431. }
  432.  
  433.  
  434. void flush (FILE *fp, short n)
  435. {
  436.   fputc (n, fp);
  437.   fwrite (code_buffer, 1, n, fp);
  438. }
  439.  
  440.  
  441. void write_code (FILE *fp, short code)
  442. {
  443.   int temp;
  444.  
  445.   byte_offset = bit_offset >> 3;
  446.   bits_left = bit_offset & 7;
  447.  
  448.   if (byte_offset >= 254) {
  449.     flush (fp, byte_offset);
  450.     code_buffer[0] = code_buffer[byte_offset];
  451.     bit_offset = bits_left;
  452.     byte_offset = 0;
  453.   }
  454.   if (bits_left > 0) {
  455.     temp = ((int) code << bits_left) | code_buffer[byte_offset];
  456.     code_buffer[byte_offset] = temp;
  457.     code_buffer[byte_offset+1] = (temp >> 8);
  458.     code_buffer[byte_offset+2] = (temp >> 16);
  459.   } else {
  460.     code_buffer[byte_offset] = code;
  461.     code_buffer[byte_offset+1] = (code >> 8);
  462.   }
  463.   bit_offset += code_size;
  464. }
  465.  
  466.  
  467. short readpixel (void)
  468. {
  469.   unsigned char pxl;
  470.  
  471.   pixctr++;
  472.   if (pixctr > maxctr)
  473.     return 9999;
  474.   pxl = *ptr;
  475.   ptr++;
  476.   return (pxl);
  477. }
  478.  
  479.  
  480. void compressImage (FILE *fp, unsigned short min_code_size)
  481. {
  482.   short prefix_code;
  483.   short suffix_char;
  484.   short hx, d;
  485.  
  486.   if (min_code_size < 2 || min_code_size > 9) {
  487.     if (min_code_size == 1)
  488.       min_code_size = 2;
  489.     else return;
  490.   }
  491.  
  492.   /* write initial code size */
  493.   fputc (min_code_size, fp);
  494.  
  495.   /* initialize the encoder */
  496.   bit_offset = 0;
  497.   init_table (min_code_size);
  498.   write_code (fp, clear_code);
  499.   if ((suffix_char = readpixel()) == 9999)
  500.     return;
  501.  
  502.   /* initialize the prefix */
  503.   prefix_code = suffix_char;
  504.  
  505.   /* get a character to compress */
  506.   while ((suffix_char = readpixel()) != 9999) {
  507.     /* derive an index into the code table */
  508.     hx = (prefix_code ^ (suffix_char << 5)) % table_size;
  509.     d = 1;
  510.  
  511.     for (;;) {
  512.       /* see if the code is in the table */
  513.       if (currentcode[hx] == 0) {
  514.         /* if not, put it there */
  515.         write_code (fp, prefix_code);
  516.         d = free_code;
  517.  
  518.         /* find the next free code */
  519.         if (free_code <= largest_code) {
  520.           oldcode[hx] = prefix_code;
  521.           newcode[hx] = suffix_char;
  522.           currentcode[hx] = free_code;
  523.           free_code++;
  524.         }
  525.         
  526.         /* expand the code size or scrap the table */
  527.         if (d == max_code) {
  528.           if (code_size < 12) {
  529.             code_size++;
  530.             max_code <<= 1;
  531.           }
  532.           else {
  533.             write_code (fp, clear_code);
  534.             init_table (min_code_size);
  535.           }
  536.         }
  537.         prefix_code = suffix_char;
  538.         break;
  539.       }
  540.       if (oldcode[hx] == prefix_code &&
  541.           newcode[hx] == suffix_char) 
  542.       {
  543.             prefix_code = currentcode[hx];
  544.             break;
  545.       }
  546.       hx += d;
  547.       d += 2;
  548.       if (hx >= table_size)
  549.         hx -= table_size;
  550.     }
  551.   }
  552.  
  553.   /* write the prefix code */
  554.   write_code (fp, prefix_code);
  555.  
  556.   /* and the end of file code */
  557.   write_code (fp, eof_code);
  558.  
  559.   /* flush the buffer */
  560.   if (bit_offset > 0) 
  561.     flush (fp, (bit_offset+7)/8);
  562.  
  563.   /* write a zero length block */
  564.   flush (fp, 0);
  565. }
  566.  
  567.  
  568. void wsavegif (char *filename, block image, color pal[256])
  569. {
  570.   GIFHEADER gh;
  571.   IMAGEBLOCK iblk;
  572.   short b;
  573.   FILE *fp;
  574.   color p[256];
  575.  
  576.   if ((fp = fopen (filename, "wb")) == NULL)
  577.     return;
  578.  
  579.   ptr = image + 4;
  580.   memset ((char *)&gh, 0, sizeof(GIFHEADER));
  581.   memcpy (gh.sig, "GIF87a", 6);
  582.   gh.screenwidth = wgetblockwidth (image);
  583.   gh.screendepth = wgetblockheight (image);
  584.   gh.flags = 0xf7;
  585.   fwrite((char *)&gh, 1, sizeof(GIFHEADER), fp);
  586.   
  587.   for (b = 0; b < 256; b++)
  588.   {
  589.     p[b].r = pal[b].r << 2;
  590.     p[b].g = pal[b].g << 2;
  591.     p[b].b = pal[b].b << 2;
  592.   }
  593.   fwrite (p, 1, 768, fp);
  594.   
  595.   memset ((char *)&iblk, 0, sizeof (IMAGEBLOCK));
  596.   fputc(',', fp);
  597.   iblk.left = 0;
  598.   iblk.top = 0;
  599.   iblk.width = gh.screenwidth;
  600.   iblk.depth = gh.screendepth;
  601.   iblk.flags = 0x7;
  602.   fwrite ((char *)&iblk, 1, sizeof (IMAGEBLOCK), fp);
  603.  
  604.   pixctr = 0;
  605.   maxctr = iblk.width * iblk.depth;
  606.   compressImage (fp, 8); 
  607.     
  608.   fputc (';', fp);
  609.   fclose (fp);
  610. }
  611.